package com.wn.task;

import com.wn.utils.Contents;
import com.wn.utils.MinioClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author yellow Docter
 * @date 2023 -06-20
 * @desc
 */
//@Component
@Slf4j
public class MyDeleteImageTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MinioClientUtil minioClientUtil;

//    @Scheduled(cron = "0/10 * * * * ?")
    public void deleteTask() {

        log.info("定时任务=====================>");
        try {
            clearImage();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public void clearImage() throws Exception {

        //01 获取差集 图片
        Set set = redisTemplate.opsForSet().difference(Contents.ALL_IMAGE, Contents.DB_IMAGE);

        //02 删除图片
        for (Object o : set) {

            minioClientUtil.removeObject(o.toString());

            //删除 redis中的 多余的图片
            redisTemplate.opsForSet().remove(Contents.ALL_IMAGE,o.toString());
            log.info("删除图片:{}", o.toString());
        }

    }

}
